package cn.hzking.gjmall.activitys.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fingdo.statelayout.StateLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.CollectBean;
import cn.hzking.gjmall.utils.OnClickEvent;

public class MyCollectActivity extends ColorStatuBarActivity {

    @Bind(R.id.back_rl)
    RelativeLayout backRl;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.refrsh)
    SwipeRefreshLayout refrsh;
    @Bind(R.id.statlayout)
    StateLayout statlayout;
    @Bind(R.id.car_allseclect)
    ImageView carAllseclect;
    @Bind(R.id.collect_topay)
    LinearLayout collectTopay;//删除按钮
    @Bind(R.id.collect_buttom_ll)
    LinearLayout collectButtomLl;
    @Bind(R.id.collect_edit)
    TextView collectEdit;
    @Bind(R.id.collect_bar1)
    RelativeLayout collectBar1;
    private boolean isEdit = false;
    private boolean isSecect = false;
    private ArrayList<CollectBean> collectBeans;
    private CollectAdapter collectAdapter;
    private ArrayList<CollectBean> totleCollects = new ArrayList<>();//总共收藏商品的list;
    private ArrayList<CollectBean> waitClearCollectPostion = new ArrayList<>();//需要删除的商品的postion


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collect);
        ButterKnife.bind(this);
        initView();
        initRecycerview();
        initListener();
    }

    private void initListener() {
        backRl.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                finish();
            }
        });
        //删除按钮
        collectTopay.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                totleCollects = collectBeans;
                int size = waitClearCollectPostion.size();
                for (int i = 0; i < size; i++) {
                    collectBeans.remove(waitClearCollectPostion.get(i));


                }
                collectAdapter.replaceData(collectBeans);
                waitClearCollectPostion.clear();
                if (collectAdapter.getData().size() == 0) {
                    statlayout.showCustomView(View.inflate(MyCollectActivity.this, R.layout.layout_statelayout_custom_collect_empty, null));
                }
            }
        });
        refrsh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
               refrsh.setRefreshing(false);
            }
        });
    }

    private void initRecycerview() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        collectAdapter = new CollectAdapter(R.layout.item_soucan_gooddetail, null);
        recyclerview.setAdapter(collectAdapter);
        collectBeans = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            collectBeans.add(new CollectBean());
        }
        collectAdapter.setNewData(collectBeans);
    }

    private void initView() {
        collectEdit.setText("编辑");
        //显示编辑的时候点击编辑编辑
        collectEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchEdit(isEdit);
            }
        });

        //显示全选的时候点击全选全选
        carAllseclect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchAll(isSecect);


            }
        });


    }

    //全选按钮
    private void switchAll(boolean isSecect1) {
        if (isSecect1) {//当前是全选,点击变成不全选
            carAllseclect.setSelected(false);
            for (int i = 0; i < collectBeans.size(); i++) {
                collectBeans.get(i).setSeclet(false);

            }
            waitClearCollectPostion.clear();
            collectAdapter.notifyDataSetChanged();
            isSecect = false;

        } else {//当前是不全选,点击变成全选
            carAllseclect.setSelected(true);
            for (int i = 0; i < collectBeans.size(); i++) {
                collectBeans.get(i).setSeclet(true);

            }
            waitClearCollectPostion.addAll(collectBeans);
            collectAdapter.notifyDataSetChanged();
            isSecect = true;
        }
    }


    private void switchEdit(boolean isEdit1) {
        if (isEdit1) {//如果正在编辑状态
            collectButtomLl.setVisibility(View.GONE);
            collectEdit.setText("编辑");
            for (int i = 0; i < collectBeans.size(); i++) {
                collectBeans.get(i).setShowImage(false);
                collectBeans.get(i).setSeclet(false);
            }
            collectAdapter.notifyDataSetChanged();
            isEdit = false;

        } else {

            collectButtomLl.setVisibility(View.VISIBLE);
            collectEdit.setText("完成");
            for (int i = 0; i < collectBeans.size(); i++) {
                collectBeans.get(i).setShowImage(true);
            }
            collectAdapter.notifyDataSetChanged();
            isEdit = true;
            isSecect = false;
            carAllseclect.setSelected(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    //内部类
    private class CollectAdapter extends BaseQuickAdapter<CollectBean, BaseViewHolder> {
        public CollectAdapter(int layoutResId, @Nullable List<CollectBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final CollectBean item) {
            final ImageView imageView = helper.getView(R.id.image_collect_item_check);
            imageView.setOnClickListener(new OnClickEvent() {
                @Override
                public void singleClick(View v) {
                    if (item.isSeclet()) {//点击之前是选中状态
                        imageView.setSelected(false);
                        item.setSeclet(false);
                        if (waitClearCollectPostion.contains(item)){
                            waitClearCollectPostion.remove(item);
                        }

                    } else {//点击之前是未选中状态
                        imageView.setSelected(true);
                        item.setSeclet(true);
                        waitClearCollectPostion.add(item);
                    }
                    if (isAllCheck()) {//如果全部选中
                        carAllseclect.setSelected(true);
                    } else {
                        carAllseclect.setSelected(false);
                    }
                    notifyItemChanged(helper.getLayoutPosition());
                }
            });
            if (item.isShowImage()) {
                imageView.setVisibility(View.VISIBLE);
            } else {
                imageView.setVisibility(View.GONE);
            }

            if (item.isSeclet()) {
                imageView.setSelected(true);
            } else {
                imageView.setSelected(false);
            }


        }
    }

    //检查是否组全部选中
    private boolean isAllCheck() {

        for (CollectBean group : collectBeans) {
            if (!group.isSeclet())
                return false;

        }

        return true;
    }

}
